package com.example.snakefinal;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Point;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Display;
import android.widget.Button;
import android.widget.EditText;


public class SnakeMultiplayer extends Activity {
// Used to display the selected skin

    @SuppressLint("UseCompatLoadingForDrawables")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.snake_multiplayer);

        EditText ip = findViewById(R.id.ip);
        EditText joinIp = findViewById(R.id.joinIp);
        joinIp.setText("10.0.2.15");

        String hostIP = DiscoverNetIpUtil.getHostIP();

        Button create = findViewById(R.id.create);
        create.setOnClickListener((e) -> {
            // Get the pixel dimensions of the screen
            Display display = getWindowManager().getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            // Create a new instance of the SnakeGame class
            SnakeGame mSnakeGame = new SnakeGame(this, size, 1);
            // Set SnakeGame as the content view of the activity
            setContentView(mSnakeGame);
            // Resume the game when the user starts the game
            mSnakeGame.resume();
            SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("SnakeGamePrefs", Context.MODE_PRIVATE);
            int skinResource = sharedPref.getInt("snakeWayMethod", 1);
            mSnakeGame.initWayMethod(skinResource);
            // 注册陀螺仪。
            if (skinResource == 2) {
                SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
                Sensor gyroscopeSensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
                sensorManager.registerListener(new ShakeSensor(mSnakeGame), gyroscopeSensor, SensorManager.SENSOR_DELAY_NORMAL);
            }
            new Thread(new Server(mSnakeGame)).start();
        });
        Button join = findViewById(R.id.join);

        join.setOnClickListener((e) -> {
            System.out.println("join");
            // Get the pixel dimensions of the screen
            Display display = getWindowManager().getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            // Create a new instance of the SnakeGame class
            SnakeGame mSnakeGame = new SnakeGame(this, size, 2);
            // Set SnakeGame as the content view of the activity
            setContentView(mSnakeGame);
            // Resume the game when the user starts the game
            mSnakeGame.resume();
            SharedPreferences sharedPref = getApplicationContext().getSharedPreferences("SnakeGamePrefs", Context.MODE_PRIVATE);
            int skinResource = sharedPref.getInt("snakeWayMethod", 1);
            mSnakeGame.initWayMethod(skinResource);
            // 注册陀螺仪。
            if (skinResource == 2) {
                SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
                Sensor gyroscopeSensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
                sensorManager.registerListener(new ShakeSensor(mSnakeGame), gyroscopeSensor, SensorManager.SENSOR_DELAY_NORMAL);
            }
            try {
                System.out.println("start client");
                new Thread(new Client(joinIp.getText().toString(), mSnakeGame)).start();
                System.out.println("end client");
            } catch (Exception exception) {
                System.out.println("服务器连接失败");
                System.out.println(exception.toString());
            }
        });


        ip.setText(hostIP);
        ip.setEnabled(false);
        // Returns the click event for the button
        Button btnBack = findViewById(R.id.multiplyBack);
        btnBack.setBackground(getResources().getDrawable(R.drawable.rounded_button));
        btnBack.setOnClickListener(v -> finish());  // End the current activity and return to the previous page

    }


}
